	
function getHelp(event){
	var xhr=null;

    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { afficherAide(xhr); };
 
    //on appelle le fichier reponse.txt

    xhr.open("GET", wotHub+'/aide.xml');
    xhr.send(null);
}

function afficherAide(xhr) {
	if (xhr.readyState==4) 
    {
    	var docXML= xhr.responseXML;
    	//echo '<team><name>'.$team['esl_name'].'</name><type>'.$team['event_id'].'</type><typeName>'.$team['nom'].'</typeName></team>';
    	var module = docXML.getElementsByTagName(getConstructeur())[0] ;
    	if (module != null) {
    		for (e=0; e < module.children.length ; e++ ) {
    			if(module.children[e].tagName == getMethode()) {
    				for (c = 0; c < module.children[e].children.length; c++) {
    					attachHelp(module.children[e].children[c]);
    				}
    			}
    		}
    	}
    	/*var elements = docXML.getElementsByTagName(getMethode()) ;
    	if (elements.length > 0) {
    		for (e = 0; e < elements[0].children.length; e++) {
        		attachHelp(elements[0].children[e]);
        	}	
    	}*/
    }
}

function attachHelp(element) {
	var help = new Array();
	for (i = 0; i < element.children.length; i++) {
		help[element.children[i].nodeName] = element.children[i].innerHTML;
	}
	switch (help.type) {
	case 'Tag':
		var selectedElem = document.getElementsByTagName(help.id)[help.indice];
		break;
	case 'Id':
		if (help.attach != null && help.attach == 'parent') {
			var selectedElem = document.getElementById(help.id).parentElement;
		}
		else {
			var selectedElem = document.getElementById(help.id);
		}
		break;
	} 
	var t = new Date().getTime();
	var imgHelp = document.createElement('img');
	imgHelp.src = wotHub+'/images/help.png';
	imgHelp.id = 'i'+t;
	imgHelp.className = 'help';
	selectedElem.appendChild(imgHelp);
	var divHelp = document.createElement('div')
	divHelp.className = "help";
	divHelp.id = "d"+t;
	var titreHelp = document.createElement('h2');
	titreHelp.innerText = help.titre;
	var close = document.createElement('img');
	close.src = wotHub+'/images/Delete.png';
	close.className='close';
	close.id = 'c'+t;
	divHelp.appendChild(close);
	divHelp.appendChild(titreHelp);
	divHelp.innerHTML += help.description;
	document.getElementsByTagName('body')[0].appendChild(divHelp);
	selectedElem.addEventListener('click', function(event) {showHelp(event)}, false);
	var btnClose = document.getElementById(close.id)
	btnClose.addEventListener('click', function(event) {document.getElementById('d'+t).style.display='none'}, false);
}

function showHelp(event) {
	var target = 'd'+event.target.id.substr(1);
	div = document.getElementById(target);
	var display = 'none'
	if (div.style.display == 'none' || div.style.display =='') {
		display = 'block';
		var coord = event.target.getBoundingClientRect();
		x = coord.left+1;
		y = coord.bottom;
		div.style.top = y+'px';
		div.style.left = x+'px';
		
	}
	var pWidth = window.innerWidth;
	div.style.display = display;
	dWidth = div.getBoundingClientRect().width;
	if (x+dWidth > pWidth) {
		div.style.left = pWidth-dWidth+'px';
	}
}

window.addEventListener('load', function(event) {
	getHelp(event)
}, false);

